package com.bsfcs.rag.service;

import com.bsfcs.rag.entity.*;
import java.lang.reflect.Field;
import java.util.*;

/**
 * 字段过滤工具类
 * 用于根据配置过滤实体类的字段
 */
public class FieldFilterUtils {
    
    /**
     * 过滤合同对象的字段
     * @param contract 合同对象
     * @param fields 要保留的字段列表
     * @return 过滤后的合同对象
     */
    public static Contract filterContractFields(Contract contract, Set<String> fields) {
        if (contract == null) {
            return null;
        }
        
        if (fields == null || fields.isEmpty()) {
            return contract;
        }
        
        Contract filteredContract = new Contract();
        copyFields(contract, filteredContract, fields);
        return filteredContract;
    }
    
    /**
     * 过滤合同履行节点列表的字段
     * @param contractLxPlans 合同履行节点列表
     * @param fields 要保留的字段列表
     * @return 过滤后的合同履行节点列表
     */
    public static List<ContractLxPlan> filterContractLxPlanFields(List<ContractLxPlan> contractLxPlans, Set<String> fields) {
        if (contractLxPlans == null) {
            return Collections.emptyList();
        }
        
        if (fields == null || fields.isEmpty()) {
            return contractLxPlans;
        }
        
        List<ContractLxPlan> filteredList = new ArrayList<>();
        for (ContractLxPlan contractLxPlan : contractLxPlans) {
            ContractLxPlan filteredPlan = new ContractLxPlan();
            copyFields(contractLxPlan, filteredPlan, fields);
            filteredList.add(filteredPlan);
        }
        return filteredList;
    }
    
    /**
     * 过滤付款记录列表的字段
     * @param contractPaymoneys 付款记录列表
     * @param fields 要保留的字段列表
     * @return 过滤后的付款记录列表
     */
    public static List<ContractPaymoney> filterContractPaymoneyFields(List<ContractPaymoney> contractPaymoneys, Set<String> fields) {
        if (contractPaymoneys == null) {
            return Collections.emptyList();
        }
        
        if (fields == null || fields.isEmpty()) {
            return contractPaymoneys;
        }
        
        List<ContractPaymoney> filteredList = new ArrayList<>();
        for (ContractPaymoney contractPaymoney : contractPaymoneys) {
            ContractPaymoney filteredPaymoney = new ContractPaymoney();
            copyFields(contractPaymoney, filteredPaymoney, fields);
            filteredList.add(filteredPaymoney);
        }
        return filteredList;
    }
    
    /**
     * 过滤开票记录列表的字段
     * @param invoices 开票记录列表
     * @param fields 要保留的字段列表
     * @return 过滤后的开票记录列表
     */
    public static List<Invoice> filterInvoiceFields(List<Invoice> invoices, Set<String> fields) {
        if (invoices == null) {
            return Collections.emptyList();
        }
        
        if (fields == null || fields.isEmpty()) {
            return invoices;
        }
        
        List<Invoice> filteredList = new ArrayList<>();
        for (Invoice invoice : invoices) {
            Invoice filteredInvoice = new Invoice();
            copyFields(invoice, filteredInvoice, fields);
            filteredList.add(filteredInvoice);
        }
        return filteredList;
    }
    
    /**
     * 过滤进项发票记录列表的字段
     * @param contractjxfpdjs 进项发票记录列表
     * @param fields 要保留的字段列表
     * @return 过滤后的进项发票记录列表
     */
    public static List<Contractjxfpdj> filterContractjxfpdjFields(List<Contractjxfpdj> contractjxfpdjs, Set<String> fields) {
        if (contractjxfpdjs == null) {
            return Collections.emptyList();
        }
        
        if (fields == null || fields.isEmpty()) {
            return contractjxfpdjs;
        }
        
        List<Contractjxfpdj> filteredList = new ArrayList<>();
        for (Contractjxfpdj contractjxfpdj : contractjxfpdjs) {
            Contractjxfpdj filteredJxfpdj = new Contractjxfpdj();
            copyFields(contractjxfpdj, filteredJxfpdj, fields);
            filteredList.add(filteredJxfpdj);
        }
        return filteredList;
    }
    
    /**
     * 复制对象字段
     * @param source 源对象
     * @param target 目标对象
     * @param fields 要复制的字段列表
     */
    private static void copyFields(Object source, Object target, Set<String> fields) {
        if (source == null || target == null || fields == null || fields.isEmpty()) {
            return;
        }
        
        Class<?> sourceClass = source.getClass();
        Class<?> targetClass = target.getClass();
        
        for (String fieldName : fields) {
            try {
                Field sourceField = sourceClass.getDeclaredField(fieldName);
                Field targetField = targetClass.getDeclaredField(fieldName);
                
                sourceField.setAccessible(true);
                targetField.setAccessible(true);
                
                Object value = sourceField.get(source);
                targetField.set(target, value);
            } catch (Exception e) {
                // 字段不存在或访问异常，跳过该字段
                System.err.println("Failed to copy field: " + fieldName + " from " + sourceClass.getSimpleName() + " to " + targetClass.getSimpleName() + ": " + e.getMessage());
            }
        }
    }
    
    /**
     * 检查是否有任何字段配置
     * @param config 配置对象
     * @return 如果有任何字段配置返回true，否则返回false
     */
    public static boolean hasAnyFieldsConfigured(ContractDataQueryConfig config) {
        if (config == null) {
            return false;
        }
        return config.hasContractFields() || config.hasContractLxPlanFields() || 
               config.hasContractPaymoneyFields() || config.hasInvoiceFields() || 
               config.hasContractjxfpdjFields();
    }
}